﻿//-------------------------------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2011 , JamSoft TECH, Ltd .
//-------------------------------------------------------------------------------------

using System;
using System.Data;
using System.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

// 系统自动生成的类库
using DotNet.Business;
using DotNet.Utilities;

/// <remarks>
/// ChangePassword
/// 修改密码
/// 
/// 修改纪录
///	版本：1.0 2011.08.30 LiangMingMing  创建代码。 
/// <author>
///     <verison>1.0</verison>
///		<name>LiangMingMing</name>
///		<date>2011.08.30</date>
///		<description>修改密码</description>
/// </author> 
/// </remarks>
public partial class UserInfo : BasePage
{
    // **********************************************************************************************
    // *                                                                                            * 
    // *                             Public Variate    公共变量                                     *
    // *                                                                                            * 
    // **********************************************************************************************

    /// <summary>
    /// 下载地址
    /// </summary>
    public string downloadUrl = string.Empty;
    /// <summary>
    /// 多语言变量（添加到收藏夹）
    /// </summary>
    public string ToolbarFavorite = string.Empty;
    /// <summary>
    /// 多语言变量（显示隐藏左边菜单）
    /// </summary>
    public string ToolbarShowhideLeftFrame = string.Empty;
    /// <summary>
    /// 多语言变量（显示隐藏TOP页面）
    /// </summary>
    public string ToolbarShowhideTopFrame = string.Empty;
    /// <summary>
    /// 多语言变量（帮助中心）
    /// </summary>
    public string ToolbarHelp = string.Empty;
    /// <summary>
    /// 多语言-按钮（保存）
    /// </summary>
    public string ButtonConfirm = string.Empty;
    /// <summary>
    /// 多语言-按钮（修改密码）
    /// </summary>
    public string ButtonChangePwd = string.Empty;

    // **********************************************************************************************
    // *                                                                                            * 
    // *                             InitializeComponent  初始化                                    * 
    // *                                                                                            * 
    // **********************************************************************************************

    #region private void PageOnLoad() 页面初次加载时的动作
    /// <summary>
    /// 页面初次加载时的动作
    /// </summary>
    private void PageOnLoad()
    {
        try
        {
            // 帮办你分
            this.BindContractYear();
            // 打开数据库连接
            this.UserCenterDbHelper.Open();
            // 获取下载地址
            this.downloadUrl = ConfigurationManager.AppSettings["DownloadUrl"];
            //显示用户信息
            this.ShowUser();
        }
        catch (Exception ex)
        {
            // 记录日志
            this.LogException(ex);
            // 抛出异常
            throw ex;
        }
        finally
        {
            // 关闭数据库连接
            this.UserCenterDbHelper.Close();
        }
    }
    #endregion

    // **********************************************************************************************
    // *                                                                                            * 
    // *                                       Method   操作方法                                    * 
    // *                                                                                            * 
    // **********************************************************************************************

    #region private void Localization() 多语言国际化加载
    /// <summary>
    /// 多语言国际化加载
    /// </summary>
    private void Localization()
    {
        // 多语言国际化加载
        ResourceManagerWrapper.Instance.LoadResources(Server.MapPath("../../../../Resource/Localization/"));
        // 获取语言信息
        this.GetLanguageResource(Utilities.GetCurrentLanguage(this.UserInfo));
    }
    #endregion

    #region private void GetLanguageResource(string languages) 获取语言
    /// <summary>
    /// 获取语言
    /// </summary>
    /// <param name="languages">语言类型</param>
    private void GetLanguageResource(string languages)
    {
        /// <以变量形式加载，刷新时会被刷掉所以得一直加载着>
        // 添加到收藏夹
        this.ToolbarFavorite = ResourceManagerWrapper.Instance.Get(languages, "Toolbar.Favorite");
        // 显示隐藏左边菜单
        this.ToolbarShowhideLeftFrame = ResourceManagerWrapper.Instance.Get(languages, "Toolbar.ShowhideLeftFrame");
        // 显示隐藏TOP页面
        this.ToolbarShowhideTopFrame = ResourceManagerWrapper.Instance.Get(languages, "Toolbar.ShowhideTopFrame");
        // 帮助中心
        this.ToolbarHelp = ResourceManagerWrapper.Instance.Get(languages, "Toolbar.Help");
        // 按钮-保存
        this.ButtonConfirm = ResourceManagerWrapper.Instance.Get(languages, "Button.Save");
        // 按钮-修改密码
        this.ButtonChangePwd = ResourceManagerWrapper.Instance.Get(languages, "UserInfo.aspx_btnChangePwd");
        /// <用的是控件，所以不怕被刷新>
        if (!Page.IsPostBack)
        {
            // 个人信息修改
            this.Title = ResourceManagerWrapper.Instance.Get(languages, "UserInfo.aspx");
            // 部门： 
            this.ltlClass.Text = ResourceManagerWrapper.Instance.Get(languages, "UserInfo.aspx_ltlClass");
            // 电子邮件：
            this.ltlEmail.Text = ResourceManagerWrapper.Instance.Get(languages, "UserInfo.aspx_ltlEmail");
            // 移动电话：
            this.ltlMobile.Text = ResourceManagerWrapper.Instance.Get(languages, "UserInfo.aspx_ltlMobile");
            // QQ：
            this.ltlQQ.Text = ResourceManagerWrapper.Instance.Get(languages, "UserInfo.aspx_ltlQQ");
            // 真实姓名：
            this.ltlRealName.Text = ResourceManagerWrapper.Instance.Get(languages, "UserInfo.aspx_ltlRealName");
            // 工号：
            this.LtlStudentID.Text = ResourceManagerWrapper.Instance.Get(languages, "UserInfo.aspx_LtlStudentID");
            // 个人信息 - 我的信息
            this.ltlTittle.Text = ResourceManagerWrapper.Instance.Get(languages, "UserInfo.aspx_ltlTittle");
            // 用户名：
            this.ltlUserName.Text = ResourceManagerWrapper.Instance.Get(languages, "UserInfo.aspx_ltlUserName");
            // 我的信息修改
            this.ltlWoDeXinXiXiuGai.Text = ResourceManagerWrapper.Instance.Get(languages, "UserInfo.aspx_ltlWoDeXinXiXiuGai");
            // 提示信息:修改成功。
            this.hdfModifySuccess.Value = ResourceManagerWrapper.Instance.Get(languages, "MSG50024");
            // 提示信息:修改失败。
            this.hdfModifyLose.Value = ResourceManagerWrapper.Instance.Get(languages, "MSG50023");
        }
    }
    #endregion

    #region private void BindContractYear() 绑定年份
    /// <summary>
    /// 绑定年份
    /// </summary>
    private void BindContractYear()
    {
        for (int i = DateTime.Now.Year - 50; i <= DateTime.Now.Year; i++)
        {
            ListItem listItem = new ListItem(i.ToString(), i.ToString());
            //this.ddlBirthYear.Items.Add(listItem);
        }
    }
    #endregion

    #region private void ShowUser(BaseUserEntity userEntity) 显示员工内容
    /// <summary>
    /// 用户对象
    /// </summary>
    /// <param name="userEntity"></param>
    private void ShowUser(BaseUserEntity userEntity)
    {
        this.txtUserName.Text = userEntity.UserName;
        this.txtRealName.Text = userEntity.RealName;
        this.txtStudentID.Text = userEntity.Code;
        this.txtClass.Text = userEntity.DepartmentName;
        //this.rblSex.SelectedValue = userEntity.Sex;

        if (!string.IsNullOrEmpty(userEntity.Birthday))
        {
            char[] flags = { '-' };
            //this.ddlBirthYear.SelectedValue = userEntity.Birthday.Split(flags)[0];
            //this.ddlBirthMonth.SelectedValue = userEntity.Birthday.Split(flags)[1];
            //this.ddlBirthDay.SelectedValue = userEntity.Birthday.Split(flags)[2];
        }
        this.txtMobile.Text = userEntity.Mobile;
        this.txtEmail.Text = userEntity.Email;
        this.txtQQ.Text = userEntity.OICQ;
    }
    #endregion

    #region private void ShowUser() 显示员工内容
    /// <summary>
    /// 显示员工内容
    /// </summary>
    private void ShowUser()
    {
        if (this.UserInfo.Id.Length > 0)
        {
            BaseUserManager userManager = new BaseUserManager(this.UserCenterDbHelper, this.UserInfo);
            //获取员工ID所约束的数据库记录
            DataTable dataTable = userManager.GetDataTableById(this.UserInfo.Id);
            BaseUserEntity userEntity = new BaseUserEntity(dataTable);
            this.ShowUser(userEntity);
        }
        else
        {
            // 这条记录已经被删除了
            Page.Response.Redirect(Utilities.NotFindPage);
        }
    }
    #endregion

    #region private bool CheckInput() 检查页面的输入
    /// <summary>
    /// 检查页面的输入
    /// </summary>
    /// <returns>是否正确</returns>
    private bool CheckInput()
    {
        string checkInput = string.Empty;
        if (this.txtUserName.Text.Length == 0)
        {
            checkInput = "<script>alert('提示信息：请输入用户名。');document.getElementById('" + this.txtUserName.ClientID + "').focus();</script>";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "checkInput", checkInput);
            return false;
        }
        return true;
    }
    #endregion

    #region private BaseUserEntity GetEntity(BaseUserEntity userEntity) 从窗体读取奖励实体
    /// <summary>
    /// 从窗体读取用户实体
    /// </summary>
    /// <returns>用户</returns>
    private BaseUserEntity GetEntity(BaseUserEntity userEntity)
    {
        userEntity.UserName = this.txtUserName.Text;
        userEntity.RealName = this.txtRealName.Text;
        userEntity.Code = this.txtStudentID.Text;
        userEntity.DepartmentName = this.txtClass.Text;
        //userEntity.Sex= this.rblSex.SelectedValue;
        //string birthday = this.ddlBirthYear.SelectedValue + "-" + this.ddlBirthMonth.SelectedValue + "-" + this.ddlBirthDay.SelectedValue;
        //userEntity.Birthday= birthday;
        userEntity.Mobile = this.txtMobile.Text;
        userEntity.Email = this.txtEmail.Text;
        userEntity.OICQ = this.txtQQ.Text;
        return userEntity;
    }
    #endregion

    #region private void ClearForm() 清空页面
    /// <summary>
    /// 清空页面
    /// </summary>
    private void ClearForm()
    {
        // this.txtUserName.Text = string.Empty;
        this.txtRealName.Text = string.Empty;
        this.txtStudentID.Text = string.Empty;
        this.txtClass.Text = string.Empty;
        //this.rblSex.ClearSelection();
        //this.ddlBirthYear.SelectedIndex = 0;
        //this.ddlBirthMonth.SelectedIndex = 0;
        //this.ddlBirthDay.SelectedIndex = 0;
        this.txtMobile.Text = string.Empty;
        this.txtEmail.Text = string.Empty;
        this.txtQQ.Text = string.Empty;
    }
    #endregion

    #region private int DoUpdate(bool clearForm) 保存员工信息
    /// <summary>
    /// 保存员工信息
    /// </summary>
    /// <returns>员工代码</returns>
    private int DoUpdate(bool clearForm)
    {
        int returnValue = 0;
        // 检查页面的输入
        if (!this.CheckInput())
        {
            return returnValue;
        }
        try
        {
            this.UserCenterDbHelper.Open();
            BaseUserManager userManager = new BaseUserManager(this.UserCenterDbHelper, this.UserInfo);
            if (this.UserInfo.Id.Length > 0)
            {
                DataTable dataTable = userManager.GetDataTableById(this.UserInfo.Id);
                BaseUserEntity userEntity = new BaseUserEntity(dataTable);
                this.GetEntity(userEntity);
                returnValue = userManager.UpdateEntity(userEntity);
            }
        }
        catch (Exception ex)
        {
            this.LogException(ex);
            throw ex;
        }
        finally
        {
            this.UserCenterDbHelper.Close();
        }
        // 显示提示信息
        if (Utilities.ShowInformation)
        {
            if (returnValue > 0)
            {
                // 提示修改成功
                this.ClientScript.RegisterStartupScript(this.GetType(), "updateSuccessed", "<script>alert('" + this.hdfModifySuccess.Value + "');document.getElementById('" + this.txtUserName.ClientID + "').focus();</script>");
            }
            else
            {
                // 提示修改失败
                this.ClientScript.RegisterStartupScript(this.GetType(), "updateFailure", "<script>alert('" + this.hdfModifyLose.Value + "');document.getElementById('" + this.txtUserName.ClientID + "').focus();</script>");
            }
        }
        return returnValue;
    }
    #endregion

    // **********************************************************************************************
    // *                                                                                            * 
    // *                                       EventArgs    事件方法                                * 
    // *                                                                                            * 
    // ********************************************************************************************** 

    #region protected new void Page_Load(object sender, EventArgs e) 页面初次加载的动作
    /// <summary>
    /// 页面初次加载的动作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        // 加载国际化多语言
        this.Localization();
        if (!Page.IsPostBack)
        {
            // 页面初次加载时执行此方法
            this.PageOnLoad();
        }
    }
    #endregion

    #region protected void btnConfirm_Click(object sender, EventArgs e) 保存
    /// <summary>
    /// 保存
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnConfirm_Click(object sender, EventArgs e)
    {
        this.DoUpdate(true);
    }
    #endregion

    #region protected void btnReset_Click(object sender, EventArgs e) 重置
    /// <summary>
    /// 重置
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnReset_Click(object sender, EventArgs e)
    {
        this.ClearForm();
    }
    #endregion

    #region protected void btnChangePwd_Click(object sender, EventArgs e) 修改密码
    /// <summary>
    /// 修改密码
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnChangePwd_Click(object sender, EventArgs e)
    {
        // 判断是否带加载效果
        if (Utilities.IsLoading)
            // 转向返回页面（带加载效果）
            Page.Response.Redirect(Utilities.LoadingPageUrl + Server.UrlEncode("Modules/Basic/Common/UserAdmin/ChangePassword.aspx"));
        else
            // 转向返回页面（带加载效果）
            Page.Response.Redirect("ChangePassword.aspx");
    }
    #endregion
}
